/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import cn.webrx.mapper.StudentMapper;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import java.io.IOException;
import java.util.Properties;

/**
 * <p>Project: mybatis2021 - MyApp
 * <p>Powered by webrx On 2021-07-19 09:50:13
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
public class MyApp {
    public static void main(String[] args) throws IOException {
        //建立数据源，
        DruidDataSource ds = new DruidDataSource();
        Properties prop = new Properties();
        prop.load(Resources.getResourceAsReader("db.properties"));
        ds.setConnectProperties(prop);

        TransactionFactory tx = new JdbcTransactionFactory();
        Environment env = new Environment("development", tx, ds);
        Configuration cfg = new Configuration(env);


        //设置类别名，cn.webrx.cn.webrx.entit 包下的Student.java 别名默认是 student
        cfg.getTypeAliasRegistry().registerAliases("cn.webrx.cn.webrx.entit");
        //cfg.getTypeAliasRegistry().registerAlias("st", "cn.webrx.cn.webrx.entit.Student");
        //cfg.getTypeAliasRegistry().registerAlias("student", "cn.webrx.cn.webrx.entit.Student");
        //如果有类还需要设置别名，找到实体类 @Alias("stu")

        cfg.addMappers("cn.webrx.mapper");
        SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(cfg);


        SqlSession ss = sf.openSession();

        StudentMapper sm = ss.getMapper(StudentMapper.class);

        System.out.println(sm.queryById(3));
    }
}
